#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
int sum =0;

void *sum_calc(void *arg)
{
     pthread_mutex_lock(&counter_mutex);
     sum++;
     pthread_mutex_unlock(&counter_mutex);
}

int main(int argc, char const *argv[])
{
    pthread_t thread_id[30000];
    for(int i=0; i<30000; i++)
    {
        pthread_create(&thread_id[i], NULL,  sum_calc , NULL);
    }

    for(int i=0; i<30000; i++)
    {
        pthread_join(thread_id[i], NULL);
    }

    printf("sum = %d\n", sum);

    return 0;
}
